package com.jmu.medicineknows.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmu.medicineknows.dto.SalesStatisticsDto;
import com.jmu.medicineknows.entity.Result;
import com.jmu.medicineknows.entity.SaleCount;
import com.jmu.medicineknows.mapper.SaleCountMapper;
import com.jmu.medicineknows.mapper.SalesStatisticsMapper;
import com.jmu.medicineknows.service.SalesStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Author: JieZi
 * @Date: 2023/7/14 09:16
 * @Description:
 **/
@Service
@SuppressWarnings("all")
public class SalesStatisticsServiceImpl implements SalesStatisticsService {
    @Autowired
    public SalesStatisticsMapper salesStatisticsMapper;
    @Autowired
    public SaleCountMapper saleCountMapper;
    @Override
    public Result countAll() {
        QueryWrapper <SaleCount> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(total) as total");
        SalesStatisticsDto salesStatisticsDto = new SalesStatisticsDto();
        String total = saleCountMapper.selectOne(queryWrapper).getTotal();
        salesStatisticsDto.setTotal(total);
        LambdaQueryWrapper <SaleCount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SaleCount> page = new Page<>(1,3);
        lambdaQueryWrapper.orderByDesc(SaleCount::getTotal);
        Page<SaleCount> countPage = saleCountMapper.selectPage(page, lambdaQueryWrapper);
        List <SaleCount> list = new ArrayList();
        for (SaleCount saleCount:countPage.getRecords()){
            list.add(saleCount);
        }
        salesStatisticsDto.setSaleList(list);
        return Result.defaultSuccess(salesStatisticsDto);
    }
}